cmake_minimum_required(VERSION 3.22.1)

project(MyGLCraft)

set(CORE ${PROJECT_NAME}-Core)

# 设置 C++17 标准
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

if (CMAKE_BUILD_TYPE MATCHES Release)
    if(MSVC)
        set(CMAKE_CXX_FLAGS "/O2 /EHsc")
    else()
        set(CMAKE_CXX_FLAGS "-O3")
    endif()
    add_definitions(-DNDEBUG)
elseif (CMAKE_BUILD_TYPE MATCHES Trace)
    if(MSVC)
        set(CMAKE_CXX_FLAGS "/O2 /EHsc")
    else()
        set(CMAKE_CXX_FLAGS "-O3")
    endif()
    add_definitions(-DENABLE_TRACING)
elseif (CMAKE_BUILD_TYPE MATCHES Debug)
    if(MSVC)
        set(CMAKE_CXX_FLAGS "/Zi /EHsc")
    else()
        set(CMAKE_CXX_FLAGS "-g")
    endif()
endif ()
add_definitions(-DENABLE_LOG)    

# 拷贝资源路径
file(GLOB ASSETS "./assets/*")

# 把 ASSETS 指代目录集合的内容都拷贝到可执行文件目录下
file(COPY ${ASSETS} DESTINATION ${CMAKE_BINARY_DIR}/assets/)

# 添加 thridParty 目录中的 glm 等头文件
set(3rd_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ext")
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${3rd_DIR}/glm
    ${3rd_DIR}/glfw/include
    ${3rd_DIR}/tinyobjloader
    ${3rd_DIR}/stbimage
    ${3rd_DIR}/entt
    ${3rd_DIR}/json
    ${3rd_DIR}/FastNoiseLite
    ${3rd_DIR}/FrustumCulling
    ${3rd_DIR}/spdlog/include
)
message(${3rd_DIR}/spdlog/include)
# 搜索当前目录下所有的 cpp 文件，加入到 SRCS 变量中
file(GLOB_RECURSE SRCS LIST_DIRECTORIES false "src/**")

# 生成可执行文件 VulkanExample，包含所有主目录中的 cpp 文件
add_library(${CORE} SHARED ${SRCS})
target_include_directories(${CORE} PUBLIC 
    src
)
target_compile_definitions(${CORE} PUBLIC
    SAVE_VERSION=3
)

# GLFW
add_subdirectory(${3rd_DIR}/glfw)
target_link_libraries(${CORE} PUBLIC
    glfw
)

# glad
add_subdirectory(${3rd_DIR}/glad)
target_link_libraries(${CORE} PUBLIC
    glad
)

# zlib
option(ZLIB_BUILD_EXAMPLES "" OFF)
add_subdirectory(${3rd_DIR}/zlib)
target_link_libraries(${CORE} PUBLIC
    zlibstatic
)

# spdlog
add_subdirectory(${3rd_DIR}/spdlog)
target_link_libraries(${CORE} PUBLIC
    spdlog
)

# imgui
add_library(
        imgui STATIC
        ${3rd_DIR}/imgui/imgui.cpp
        ${3rd_DIR}/imgui/imgui.h
        ${3rd_DIR}/imgui/imgui_demo.cpp
        ${3rd_DIR}/imgui/imgui_draw.cpp
        ${3rd_DIR}/imgui/imgui_internal.h
        ${3rd_DIR}/imgui/imgui_widgets.cpp
        ${3rd_DIR}/imgui/imstb_rectpack.h
        ${3rd_DIR}/imgui/imstb_textedit.h
        ${3rd_DIR}/imgui/imstb_truetype.h
        ${3rd_DIR}/imgui/imconfig.h
        ${3rd_DIR}/imgui/imgui_tables.cpp
        ${3rd_DIR}/imgui/backends/imgui_impl_glfw.h
        ${3rd_DIR}/imgui/backends/imgui_impl_glfw.cpp
        ${3rd_DIR}/imgui/backends/imgui_impl_opengl3.h
        ${3rd_DIR}/imgui/backends/imgui_impl_opengl3.cpp
)
target_include_directories(imgui PUBLIC ${3rd_DIR}/imgui ${3rd_DIR}/imgui/backends)
target_link_libraries(imgui PUBLIC glfw)
target_link_libraries(${CORE} PUBLIC
    imgui
)
# Tracy
# option (TRACY_ENABLE "" ON )
# option (TRACY_ON_DEMAND "" ON )
# add_subdirectory(ext/tracy)
# target_include_directories(${CORE} PRIVATE ext/tracy/public)
# if(WIN32)
#     target_link_libraries(${CORE} PRIVATE Tracy::TracyClient)
# else()
#     target_link_libraries(${CORE} PRIVATE Tracy::TracyClient dl pthread)
# endif()

# PCH
target_precompile_headers(${CORE} PUBLIC
    src/Core/Base.h
)

add_executable(${PROJECT_NAME} Main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC
    ${CORE}

)
message(${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/assets/)
# 编译后命令：将assets文件夹赋值到二进制文件目录下
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/assets/
        ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/assets/
)

add_subdirectory(plugins/MoreBlocks)
add_subdirectory(plugins/MoreGui)